using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SongsDB;

namespace com.savage.mmtoxbmcexporter
{
    public partial class XBMCExporterForm : Form
    {
        ISDBApplication m_SDB;

        public XBMCExporterForm()
        {
            InitializeComponent();

            m_SDB = new SDBApplicationClass();
            m_SDB.ShutdownAfterDisconnect = false;

            FillTreeView();
        }

        private void FillTreeView()
        {
            ISDBPlaylist playlistRootNode = m_SDB.get_PlaylistByTitle("");
            AddPlaylists(playlistRootNode, m_playlistsTree.Nodes);
            m_playlistsTree.ExpandAll();
        }

        private void AddPlaylists(ISDBPlaylist parent, TreeNodeCollection parentCollection)
        {
            string title = parent.Title;
            TreeNodeCollection newColl;
            if (title == "" || title == null)
            {
                newColl = parentCollection;
            }
            else
            {
                TreeNode newNode = new TreeNode();
                newNode.Name = title;
                newNode.Text = title;
                newNode.Tag = parent;
                parentCollection.Add(newNode);
                newColl = newNode.Nodes;
            }
            SDBPlaylists childlists = parent.ChildPlaylists;
            for (int i = 0; i < childlists.Count; ++i)
            {
                AddPlaylists(childlists.get_Item(i), newColl);
            }
        }

        private void m_selectAll_Click(object sender, EventArgs e)
        {
            CheckAll(true, m_playlistsTree.Nodes);
        }

        

        private void m_selectNone_Click(object sender, EventArgs e)
        {
            CheckAll(false, m_playlistsTree.Nodes);
        }

        private void CheckAll(bool value, TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                node.Checked = value;
                CheckAll(value, node.Nodes);
            }
        }

        private void m_playlistsTree_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked == true)
            {
                // check all children
                CheckAll(true, e.Node.Nodes);
            }
        }

        

        private void m_substPaths_CheckedChanged(object sender, EventArgs e)
        {
            bool subst = m_substPaths.Checked;
            m_replacedText.Enabled = subst;
            m_replacerText.Enabled = subst;
        }



        private void m_CopyPlaylists_ExportToXbox_Click(object sender, EventArgs e)
        {
            XBMCPlaylistExporter exporter = new XBMCPlaylistExporter(m_SDB, m_replacedText.Text, m_replacerText.Text, m_XboxHostname.Text,
                m_userName.Text, m_password.Text);

        }

        private void m_CopyMusic_ExportToXbox_Click(object sender, EventArgs e)
        {
            // traverse tree to get selected playlists
            List<ISDBPlaylist> playlistList = GetSelectedPlaylists();
            Console.WriteLine(string.Format("selected playlists: {0}", playlistList.Count));
            foreach (ISDBPlaylist playlist in playlistList)
            {
                Console.WriteLine(playlist.Title);
            }

            FTPUtils ftpUtils = new FTPUtils(m_XboxHostname.Text, m_userName.Text, m_password.Text);
            if (m_CopyMusic_ClearTargetDirs.Checked)
            {
                if (MessageBox.Show(string.Format("Are you sure you want to erase the directory '{0}' ?", m_CopyMusic_TargetMusicDir.Text), "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ftpUtils.ClearDirectory(m_CopyMusic_TargetMusicDir.Text);
                }
                if (MessageBox.Show(string.Format("Are you sure you want to erase the directory '{0}' ?", m_CopyMusic_TargetPlaylistsDir.Text), "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ftpUtils.ClearDirectory(m_CopyMusic_TargetPlaylistsDir.Text);
                }
            }

            XBMCMusicExporter exporter = new XBMCMusicExporter(m_SDB, m_XboxHostname.Text, m_userName.Text, m_password.Text);
            exporter.Export(playlistList, m_CopyMusic_TargetMusicDir.Text, m_CopyMusic_TargetPlaylistsDir.Text);

        }

        private List<ISDBPlaylist> GetSelectedPlaylists()
        {
            return GetSelectedPlaylists(m_playlistsTree.Nodes);
        }

        private List<ISDBPlaylist> GetSelectedPlaylists(TreeNodeCollection nodeCollection)
        {
            List<ISDBPlaylist> playlistList = new List<ISDBPlaylist>();
            foreach (TreeNode node in nodeCollection)
            {
                if (node.Checked)
                {
                    playlistList.Add((ISDBPlaylist)node.Tag);
                }
                playlistList.AddRange(GetSelectedPlaylists(node.Nodes));
            }
            return playlistList;
        }

        
    }








    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new XBMCExporterForm());
        }
    }
}